// bombchain.txt
// A magical bomb which explodes if it receives
// a message of a color equal its own.

// Cell 0 - Unused.
// Cell 1, 2 - SDF. Leave both at 0 for no SDF.

beginterrainscript;
variables;
    int my_cur_mess;
    int color, active = 0, working = 1;
    int red = 2000, green = 2001, blue = 2002;
body;
beginstate INIT_STATE;
    set_script_mode(2);
    if (terrain_in_this_spot() == 490)
        color = red;
    if (terrain_in_this_spot() == 491)
        color = green;
    if (terrain_in_this_spot() == 492)
        color = blue;
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
        if (get_flag(get_memory_cell(1), get_memory_cell(2)) != 0)
        {
            set_terrain(my_loc_x(), my_loc_y(), 137);
            set_terrain(my_loc_x() - 1, my_loc_y(), 137);
    		set_terrain(my_loc_x(), my_loc_y() - 1, 137);
    		set_terrain(my_loc_x() + 1, my_loc_y(), 137);
    		set_terrain(my_loc_x(), my_loc_y() + 1, 137);
        }
break;
beginstate START_STATE;
    my_cur_mess = my_current_message();
    if ((terrain_in_this_spot() == 137) || (terrain_in_this_spot() == 137))
        working = 0;
    if (working == 0)
        end();
    if (my_cur_mess == color)
        active = 1;
    if ((active == 1) && (working == 1)) {
        if (color == red)
            set_state_continue(3);
        if (color == green)
            set_state_continue(4);
        if (color == blue)
            set_state_continue(5);
        }
break;
beginstate 3;
    put_effect_on_space(my_loc_x() - 1, my_loc_y(), 1, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() - 1, 1, 3, 1);
    put_effect_on_space(my_loc_x() + 1, my_loc_y(), 1, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() + 1, 1, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y(), 1, 10, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 1, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 1, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 1, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 1, 1);
    print_str("You have triggered a magical bomb.");
    set_terrain(my_loc_x(), my_loc_y(), 137);
    set_terrain(my_loc_x() - 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() - 1, 137);
    set_terrain(my_loc_x() + 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() + 1, 137);
    run_animation_sound(152);
    damage_nearby(5000, 2, 1, 2);
    damage_nearby(get_ran(1, 2, 16) * 5, 5, 1, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0)) 
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate 4;
    put_effect_on_space(my_loc_x() - 1, my_loc_y(), 2, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() - 1, 2, 3, 1);
    put_effect_on_space(my_loc_x() + 1, my_loc_y(), 2, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() + 1, 2, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y(), 2, 10, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 6, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 6, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 6, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 6, 1);
    print_str("You have triggered a magical bomb.");
    set_terrain(my_loc_x(), my_loc_y(), 137);
    set_terrain(my_loc_x() - 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() - 1, 137);
    set_terrain(my_loc_x() + 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() + 1, 137);
    run_animation_sound(152);
    damage_nearby(5000, 2, 6, 2);
    damage_nearby(get_ran(1, 2, 16) * 5, 5, 6, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0)) 
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate 5;
    put_effect_on_space(my_loc_x() - 1, my_loc_y(), 0, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() - 1, 0, 3, 1);
    put_effect_on_space(my_loc_x() + 1, my_loc_y(), 0, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y() + 1, 0, 3, 1);
    put_effect_on_space(my_loc_x(), my_loc_y(), 0, 10, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 5, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 5, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 5, 1);
    put_boom_on_space(my_loc_x(), my_loc_y(), 5, 1);
    print_str("You have triggered a magical bomb.");
    set_terrain(my_loc_x(), my_loc_y(), 137);
    set_terrain(my_loc_x() - 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() - 1, 137);
    set_terrain(my_loc_x() + 1, my_loc_y(), 137);
    set_terrain(my_loc_x(), my_loc_y() + 1, 137);
    run_animation_sound(152);
    damage_nearby(5000, 2, 5, 2);
    damage_nearby(get_ran(1, 2, 16) * get_memory_cell(0), 5, 5, 2);
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0)) 
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
    set_state(START_STATE);
break;
beginstate UNLOCK_SPELL_STATE;
   if (get_flag(111, 11) == 1)
   {
    active = 1;
    set_state_continue(START_STATE);
   }
break;
beginstate DISPEL_BARRIER_STATE;
   if (get_flag(111, 11) == 1)
   {
    put_effect_on_space(my_loc_x(), my_loc_y(), 10, 10, 0);
    run_animation_sound(163);
    set_terrain(my_loc_x(), my_loc_y(), 0);
    working = 0;
    if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0)) 
        set_flag(get_memory_cell(1), get_memory_cell(2), 1);
   }
break;